package com.six.firstpage.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.core.entity.Link;
import com.six.core.vo.Result;
import com.six.firstpage.service.LinkService;
import com.six.firstpage.vo.SearchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:zzh
 * @create: 2023-02-28 09:52
 * @Description:
 */
@Api(value = "firstpage/api/link", tags = {"链接管理前台Api"})
@RestController
@RequestMapping("firstpage/api/link")
//@CrossOrigin
public class ApiLinkController {
    @Autowired
    private LinkService linkService;

    @GetMapping("/getAllLink")
    public Result getAllLink(){
        List<Link> allLink = linkService.getAllLink();
        return new Result(2000,"查询友情链接成功",allLink);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SearchVo", name = "searchVo", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "current", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageSize", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("getLinkByName/{current}/{pageSize}")
    public Result getLinkByName(@RequestBody SearchVo searchVo, @PathVariable Integer current, @PathVariable Integer pageSize ){
        Page<Link> link = linkService.getLinkByName(searchVo.getTitle(), searchVo.getStatus(), current, pageSize);
        return new Result(2000,"查询链接成功",link);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Link", name = "link", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("addLink")
    public Result addLink(@RequestBody Link link){
        Integer integer = linkService.addLink(link);
        if (integer>0){
            return new Result(2000,"添加链接成功");
        }
        return new Result(5000,"添加链接失败");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Link", name = "link", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "PUT")
    @PutMapping("updateLink")
    public Result updateLink(@RequestBody Link link){
        Integer integer = linkService.updateLink(link);
        if (integer>0){
            return new Result(2000,"修改链接成功");
        }
        return new Result(5000,"修改链接失败");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "linkId", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "GET")
    @GetMapping("delLink/{linkId}")
    public Result delLink(@PathVariable Integer linkId){
        Integer integer = linkService.delLink(linkId);
        if (integer>0){
            return new Result(2000,"删除链接成功");
        }
        return new Result(5000,"删除链接失败");
    }

}
